package com.kuke.kukeproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kuke.kukeproduct.bo.ProductBO;
import com.kuke.kukeproduct.entity.Product;
import com.kuke.kukeproduct.mapper.ProductMapper;
import com.kuke.kukeproduct.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  serviceimpl
 *
 * @author kuke
 * @since 2021-08-20
 */
@Service
@Slf4j
//@RefreshScope
@CacheConfig(cacheNames = "kukeproduct")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

//    @Value("${producer.product:123}")
//    private String value;

    @Override
    public boolean add(Product product) {
        return this.save(product);
    }

    @Override
    @CacheEvict(key = "#root.targetClass+'-'+#id")
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(key = "#root.targetClass+'-'+#product.id")
    public boolean update(Product product) {
        return this.updateById(product);
    }

    @Override
    @Cacheable(key = "#root.targetClass+'-'+#id")
    public Product get(Long id) {
//        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    public List<Product> query(ProductBO productBo) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productName", productBo.getProductName());
        return this.list(queryWrapper);
    }
}
